﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DingTalkAgent.DBUtility;
namespace DingTalkAgent.SqlData
{
    //T_SysPage系统页面
    public partial class SysPage
    {
        #region	 T_SysPage

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int Page_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_SysPage");
            strSql.Append(" where ");
            strSql.Append(" Page_ID = @Page_ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Page_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Page_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条T_SysPage数据
        /// </summary>
        public int Add(DingTalkAgent.Model.SysPage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_SysPage(");
            strSql.Append("Site_ID,Code,Name,Parent_ID,OrderID,Remark,Controller,Action,active");
            strSql.Append(") values (");
            strSql.Append("@Site_ID,@Code,@Name,@Parent_ID,@OrderID,@Remark,@Controller,@Action,@active");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@Site_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.NVarChar,64) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,1024) ,
                        new SqlParameter("@Controller", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Action", SqlDbType.VarChar,64) ,
                        new SqlParameter("@active", SqlDbType.Bit,1),
                        new SqlParameter("@Code", SqlDbType.VarChar,12)

            };

            parameters[0].Value = model.Site_ID;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Parent_ID;
            parameters[3].Value = model.OrderID;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.Controller;
            parameters[6].Value = model.Action;
            parameters[7].Value = model.active;
            parameters[8].Value = model.Code;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_SysPage数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.SysPage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_SysPage set ");

            strSql.Append(" Site_ID = @Site_ID , ");
            strSql.Append(" Code = @Code , ");
            strSql.Append(" Name = @Name , ");
            strSql.Append(" Parent_ID = @Parent_ID , ");
            strSql.Append(" OrderID = @OrderID , ");
            strSql.Append(" Remark = @Remark , ");
            strSql.Append(" Controller = @Controller , ");
            strSql.Append(" Action = @Action , ");
            strSql.Append(" active = @active  ");
            strSql.Append(" where Page_ID=@Page_ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@Page_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Site_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.NVarChar,64) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,1024) ,
                        new SqlParameter("@Controller", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Action", SqlDbType.VarChar,64) ,
                        new SqlParameter("@active", SqlDbType.Bit,1)             ,
                        new SqlParameter("@Code", SqlDbType.VarChar,12)

            };

            parameters[0].Value = model.Page_ID;
            parameters[1].Value = model.Site_ID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Parent_ID;
            parameters[4].Value = model.OrderID;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.Controller;
            parameters[7].Value = model.Action;
            parameters[8].Value = model.active;
            parameters[9].Value = model.Code;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_SysPage数据
        /// </summary>
        public bool Delete(int Page_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysPage ");
            strSql.Append(" where Page_ID=@Page_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Page_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Page_ID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_SysPage数据
        /// </summary>
        public bool DeleteList(string Page_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysPage ");
            strSql.Append(" where ID in (" + Page_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.SysPage GetModel(int Page_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Page_ID, Site_ID,Code, Name, Parent_ID, OrderID, Remark, Controller, Action, active  ");
            strSql.Append("  from T_SysPage ");
            strSql.Append(" where Page_ID=@Page_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Page_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Page_ID;


            DingTalkAgent.Model.SysPage model = new DingTalkAgent.Model.SysPage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(ds.Tables[0].Rows[0]["Page_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(ds.Tables[0].Rows[0]["Site_ID"].ToString());
                }
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                if (ds.Tables[0].Rows[0]["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(ds.Tables[0].Rows[0]["Parent_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                model.Controller = ds.Tables[0].Rows[0]["Controller"].ToString();
                model.Action = ds.Tables[0].Rows[0]["Action"].ToString();
                if (ds.Tables[0].Rows[0]["active"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["active"].ToString() == "1") || (ds.Tables[0].Rows[0]["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_SysPage列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_SysPage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_SysPage列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.SysPage> IEnumGetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Page_ID, Site_ID,Code, Name, Parent_ID, OrderID, Controller, Action, active ");
            strSql.Append(" FROM T_SysPage ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.SysPage model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysPage();
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                if (dr["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(dr["Site_ID"].ToString());
                }
                model.Code = dr["Code"].ToString();
                model.Name = dr["Name"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }
                // model.Remark = dr["Remark"].ToString();
                model.Controller = dr["Controller"].ToString();
                model.Action = dr["Action"].ToString();
                if (dr["active"].ToString() != "")
                {
                    if ((dr["active"].ToString() == "1") || (dr["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }

                yield return model;
            }
        }

        /// <summary>
        /// 获得数据 T_SysPage分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="Site_ID">站点ID</param>
        /// <param name="Name">页面名称，模糊查询</param>
        /// <param name="Code">页面代码，当Getchild时传页面代码，否则传——————的长度，表示占位数据。选择出所有等在当前之上 的</param>
        /// <param name="Getchild">代表是查子集，还是</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.SysPage> GetPageList(int PageIndex, int PageSize, out int total, int Site_ID, string Name, string Code, bool Getchild)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(Page_ID) From T_SysPage ");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY Page_ID Desc) AS row, Page_ID, Site_ID,Code, Name, Parent_ID, OrderID, Controller, Action, active ");
            strSql.Append(" FROM  T_SysPage ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where Site_ID=@Site_ID ");
            if (Name.Trim() != "")
            {
                Where.Append(" and Name like @Name ");
            }
            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@Site_ID", SqlDbType.Int,4),
                    new SqlParameter("@Name", SqlDbType.VarChar,64),
                    new SqlParameter("@Code", SqlDbType.VarChar,12),
                    new SqlParameter("@total", SqlDbType.Int,4)
                                        };
            parameters[0].Value = Site_ID;
            parameters[1].Value = "%" + Name + "%";
            if (Getchild)
            {
                Where.Append(" and Code like @Code ");
                parameters[2].Value = Code + "%";
            }
            else
            {
                Where.Append(" and LEN(Code)<=" + (Code.Length + 3));
                parameters[2].Value = Code;
            }

            parameters[3].Direction = ParameterDirection.Output;


            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            List<DingTalkAgent.Model.SysPage> list = new List<DingTalkAgent.Model.SysPage>();
            DingTalkAgent.Model.SysPage model = new DingTalkAgent.Model.SysPage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[3].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysPage();
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                if (dr["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(dr["Site_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }
                model.Code = dr["Code"].ToString();
                model.Controller = dr["Controller"].ToString();
                model.Action = dr["Action"].ToString();
                if (dr["active"].ToString() != "")
                {
                    if ((dr["active"].ToString() == "1") || (dr["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 计算出当前级的下一页面代码编码
        /// </summary>
        /// <param name="code">当前级代码</param>
        /// <returns></returns>
        public string PageCode(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Max(Code) from T_SysPage");
            strSql.Append(" where ");
            strSql.Append(" Code  like @Code  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Code", SqlDbType.VarChar,12)
};
            string c = string.Empty;
            for (int s = 0; s < code.Length; s++)
            {
                c += "_";
            }
            c += "___";
            parameters[0].Value = c;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            string maxCode = obj == null ? "" : obj.ToString();
            //当不存在时，
            if (string.IsNullOrEmpty(maxCode))
            {
                return code + "001";
            }
            //当存在时
            else
            {
                int max = Convert.ToInt32(maxCode.Substring(code.Length, 3));
                max++;
                if (max < 10)
                {
                    return code + "00" + max;
                }
                if (max < 100)
                {
                    return code + "0" + max;
                }
                if (max < 1000)
                {
                    return code + "" + max;
                }
                return code + "000" + max;
            }
        }
        #endregion
    }
}
